package com.shsh.safemanage.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;

import com.shsh.safemanage.dao.DaoCreator;
import com.shsh.safemanage.dao.DepartmentDao;
import com.shsh.safemanage.dao.EquipmentDao;
import com.shsh.safemanage.dao.ProblemDao;
import com.shsh.safemanage.dao.RoleDao;
import com.shsh.safemanage.dao.UserDao;
import com.shsh.safemanage.dao.WeakDao;
import com.shsh.safemanage.model.Department1;
import com.shsh.safemanage.model.Problem;
import com.shsh.safemanage.model.ProblemTransfer;
import com.shsh.safemanage.model.Role;
import com.shsh.safemanage.model.User;
import com.shsh.safemanage.model.WeakCorpReport;
import com.shsh.safemanage.model.WeakProblem;
import com.shsh.safemanage.model.WeakReport;
import com.shsh.safemanage.model.WeakUserReport;
import com.shsh.safemanage.util.Constants;
import com.shsh.safemanage.util.MessageUtil;
import com.shsh.safemanage.util.RequestUtil;
import com.shsh.safemanage.util.Util;

public final class WeakAction {
	private boolean createNewProblem(HttpServletRequest request, Long depart1ID, Long equipID, int problemType) {
		HttpSession session = request.getSession();
		WriteProblemAction writeProblemAction = ActionCreator.createWriteProblemAction();

		boolean isSuccess;

		User user = (User) session.getAttribute(Constants.SESSION_USER);

		String problemTitle = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TITLE);
		String problemContent = null;
		String problemZhenggai = null;
		Long problemEquipID = 0l;
		if (equipID != null) {
			problemEquipID = equipID;
		}

		Problem problem = new Problem();
		problem.setDepartment1ID(depart1ID);
		problem.setEquipmentID(problemEquipID);
		problem.setProblemContent(problemContent);
		problem.setProblemTitle(Util.isoToUTF8(problemTitle));
		problem.setProblemZhenggai(problemZhenggai);
		problem.setProcessStatus(Constants.PROBLEM_STATUS_ZUZHANG_ZHUANGZHIZHANG);
		problem.setWriteTime(Calendar.getInstance());
		problem.setWriteUserID(user.getId());
		problem.setWriterDepartment1ID(user.getDepartment1ID());
		problem.setWriterDepartment2ID(user.getDepartment2ID());

		request.setAttribute(Constants.REQUEST_ATTRIBUTE_NEW_PROBLEM, problem);

		ProblemDao problemDao = DaoCreator.createProblemDao();

		isSuccess = problemDao.addProblem(problem);
		Long problemID = problem.getId();

		problemDao.updateProblem(problemID, "PROBLEM_TYPE", problemType);

		// transfer to users
		List<Long> informedUserIDList = new ArrayList<Long>();
		informedUserIDList.add(user.getId());
		writeProblemAction.addTransferInManageGroups(session, user, problem, informedUserIDList, false, true, null);

		isSuccess = writeProblemAction.createProblemTransfer(request, problem.getId());
		if (!isSuccess) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_WRITE_FAIL);
			problemDao.deleteProblemByIDList(new Long[] { problemID });
			return false;
		}
		ProblemTransfer transfer = problemDao.getProblemTransfer(user.getId(), problemID, user.getId());
		isSuccess = writeProblemAction.feedbackProblem(request, String.valueOf(problemID), String.valueOf(transfer.getId()));
		if (!isSuccess) {
			problemDao.deleteProblemByIDList(new Long[] { problemID });
			// Feedback already set error information.
			return false;
		}

		return true;
	}

	public boolean feedbackWeakReport(HttpServletRequest request) {
		HttpSession session = request.getSession();
		User sessionUser = (User) session.getAttribute(Constants.SESSION_USER);
		int processMethod = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_PROCESS_METHOD, 0);

		RoleDao roleDao = DaoCreator.createRoleDao();

		WeakReport report = (WeakReport) session.getAttribute(Constants.SESSION_WEAK_REPORT);
		if (report == null) {
			return false;
		}
		if (!roleDao.hasRight(getAuthType(report), sessionUser.getRoleID(), processMethod)) {
			switch (processMethod) {
			case Constants.PROCESS_METHOD_CLOSE:
				request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_IS_NOT_AUTHORIZED_CLOSE);
				break;
			case Constants.PROCESS_METHOD_ESCALATE:
				request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_IS_NOT_AUTHORIZED_ESCALATE);
				break;
			}
			return false;
		}

		if (!refreshWeakReport(request, report)) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NONEWORKTIME_NEEDED);
			return false;
		}
		if (!refreshWeakReport(request, report, true)) {
			return false;
		}

		Long problemID = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_ID, 0l);

		ProblemDao problemDao = DaoCreator.createProblemDao();
		WeakDao weakDao = DaoCreator.createWeakDao();

		Problem problem = problemDao.getProblemByID(problemID);

		WriteProblemAction writeProblemAction = ActionCreator.createWriteProblemAction();

		if (!writeProblemAction.canbeProcessed(request, Constants.PROBLEM_TYPE_WEAK_REPORT, sessionUser, problem)) {
			return false;
		}

		boolean isSuccess = writeProblemAction.feedbackProblem(request);
		if (!isSuccess) {
			return false;
		}

		String problemTitle = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TITLE);
		String problemContent = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_CONTENT);

		if (!Util.isNullOrEmpty(problemTitle)) {
			problemTitle = Util.isoToUTF8(problemTitle);
		}

		if (!Util.isNullOrEmpty(problemContent)) {
			problemContent = Util.isoToUTF8(problemContent);
		}

		if (processMethod == Constants.PROCESS_METHOD_CLOSE && report.getReportType() == Constants.WEAK_REPORT_TYPE_DANWEI) {
			WeakReport.setPointTable(report);
		}

		boolean isSuccess2 = weakDao.updateWeakReport(problemID, report);
		if (!isSuccess2) {
			return false;
		}

		session.removeAttribute(Constants.SESSION_WEAK_REPORT);
		return isSuccess2;
	}

	private int getAuthType(WeakReport report) {
		if (report.getReportType() == Constants.WEAK_REPORT_TYPE_YIXIANYUANGONG) {
			return Constants.AUTH_TYPE_WEAK_REPORT_YIXIAN;
		}
		if (report.getReportType() == Constants.WEAK_REPORT_TYPE_GUANLIRENYUAN) {
			return Constants.AUTH_TYPE_WEAK_REPORT_GUANLI;
		}
		if (report.getReportType() == Constants.WEAK_REPORT_TYPE_DANWEI) {
			return Constants.AUTH_TYPE_WEAK_REPORT_DANWEI;
		}

		return 0;
	}

	public boolean writeWeakReport(HttpServletRequest request) {
		HttpSession session = request.getSession();

		UserAction userAction = ActionCreator.createUserAction();
		RoleDao roleDao = DaoCreator.createRoleDao();
		Role userRole = userAction.getUserRole(request);
		int processMethod = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_PROCESS_METHOD, 0);

		WeakReport report = (WeakReport) session.getAttribute(Constants.SESSION_WEAK_REPORT);
		if (report == null) {
			return false;
		}

		if (!roleDao.hasRight(getAuthType(report), userRole.getId(), processMethod)) {
			switch (processMethod) {
			case Constants.PROCESS_METHOD_CLOSE:
				request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_IS_NOT_AUTHORIZED_CLOSE);
				break;
			case Constants.PROCESS_METHOD_ESCALATE:
				if (report.getReportType() == Constants.WEAK_REPORT_TYPE_GUANLIRENYUAN) {
					request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR,
							Constants.ERROR_KEY_IS_NOT_AUTHORIZED_GUANLIRENYUAN_WEAK_ESCALATE);
				} else {
					request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_IS_NOT_AUTHORIZED_ESCALATE);
				}
				break;
			}
			return false;
		}

		if (!refreshWeakReport(request, report)) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NONEWORKTIME_NEEDED);
			return false;
		}

		if (!refreshWeakReport(request, report, false)) {
			return false;
		}

		WeakDao weakDao = DaoCreator.createWeakDao();
		ProblemDao problemDao = DaoCreator.createProblemDao();

		boolean isSuccess = this.createNewProblem(request, report.getDepart1ID(), report.getEquipID(), Constants.PROBLEM_TYPE_WEAK_REPORT);
		Problem newProblem = (Problem) request.getAttribute(Constants.REQUEST_ATTRIBUTE_NEW_PROBLEM);
		request.removeAttribute(Constants.REQUEST_ATTRIBUTE_NEW_PROBLEM);
		if (!isSuccess) {
			return false;
		}

		report.setProblemID(newProblem.getId());
		if (report.getReportType() == Constants.WEAK_REPORT_TYPE_YIXIANYUANGONG) {
			for (WeakUserReport userReport : report.getYixianReportList()) {
				userReport.setProblemID(newProblem.getId());
			}
		}
		if (processMethod == Constants.PROCESS_METHOD_CLOSE && report.getReportType() == Constants.WEAK_REPORT_TYPE_DANWEI) {
			WeakReport.setPointTable(report);
		}

		isSuccess = weakDao.addWeakReport(report);
		if (!isSuccess) {
			problemDao.deleteProblemByIDList(new Long[] { newProblem.getId() });
			return false;
		}

		session.removeAttribute(Constants.SESSION_WEAK_REPORT);

		return true;
	}

	private boolean refreshWeakReport(HttpServletRequest request, WeakReport report) {
		String paramJSON = request.getParameter(Constants.REQUEST_PARAMETER_PARAM_JSON);
		JSONObject jsonObj = null;
		try {
			jsonObj = new JSONObject(paramJSON);

			Long problemID = null;
			if (JSONObject.getNames(jsonObj) == null) {
				return true;
			}
			for (String key : JSONObject.getNames(jsonObj)) {
				String value = jsonObj.getString(key);

				try {
					if (key.indexOf("noneWorkTimeValue") >= 0) {
						problemID = Util.extractEntityID(key, "noneWorkTimeValue");
						WeakProblem problem = report.getProblem(problemID);
						if (problem != null) {
							if (problem.hasNoneWorkTime() && Util.isNullOrEmpty(value)) {
								return false;
							}
						}
						if (value != null) {
							value = Util.isoToUTF8(value);
						}
						report.setNoneWorkTimeValue(problemID, value);
					}
				} catch (Exception e) {
					continue;
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	private boolean refreshWeakReport(HttpServletRequest request, WeakReport report, boolean clearNeedDelete) {
		// Refresh needDelete
		switch (report.getReportType()) {
		case Constants.WEAK_REPORT_TYPE_YIXIANYUANGONG:
			for (WeakUserReport userReport : report.getYixianReportList()) {
				Iterator<WeakProblem> iter = userReport.getProblemList().iterator();
				while (iter.hasNext()) {
					WeakProblem problem = iter.next();
					if (problem.isNeedDelete()) {
						report.getNeedDeleteList().add(problem);
						iter.remove();
					}
				}

				int pointSum = 0;
				for (WeakProblem problem : userReport.getProblemList()) {
					pointSum += problem.getPoint();
				}
				userReport.setPointSum(pointSum);
			}
			break;
		case Constants.WEAK_REPORT_TYPE_GUANLIRENYUAN:
			for (WeakUserReport userReport : report.getGuanliReportList()) {
				Iterator<WeakProblem> iter = userReport.getProblemList().iterator();
				while (iter.hasNext()) {
					WeakProblem problem = iter.next();
					if (problem.isNeedDelete()) {
						report.getNeedDeleteList().add(problem);
						iter.remove();
					}
				}

				int pointSum = 0;
				for (WeakProblem problem : userReport.getProblemList()) {
					pointSum += problem.getPoint();
				}
				userReport.setPointSum(pointSum);
			}
			break;
		case Constants.WEAK_REPORT_TYPE_DANWEI:
			for (WeakUserReport userReport : report.getYixianReportList()) {
				Iterator<WeakProblem> iter = userReport.getProblemList().iterator();
				while (iter.hasNext()) {
					WeakProblem problem = iter.next();
					if (problem.isNeedDelete()) {
						report.getNeedDeleteList().add(problem);
						iter.remove();
					}
				}

				int pointSum = 0;
				for (WeakProblem problem : userReport.getProblemList()) {
					pointSum += problem.getPoint();
				}
				userReport.setPointSum(pointSum);
			}
			for (WeakUserReport userReport : report.getGuanliReportList()) {
				Iterator<WeakProblem> iter = userReport.getProblemList().iterator();
				while (iter.hasNext()) {
					WeakProblem problem = iter.next();
					if (problem.isNeedDelete()) {
						report.getNeedDeleteList().add(problem);
						iter.remove();
					}
				}

				int pointSum = 0;
				for (WeakProblem problem : userReport.getProblemList()) {
					pointSum += problem.getPoint();
				}
				userReport.setPointSum(pointSum);
			}
			for (WeakUserReport userReport : report.getNotWrittenGuanliReportList()) {
				Iterator<WeakProblem> iter = userReport.getProblemList().iterator();
				while (iter.hasNext()) {
					WeakProblem problem = iter.next();
					if (problem.isNeedDelete()) {
						report.getNeedDeleteList().add(problem);
						iter.remove();
					}
				}

				int pointSum = 0;
				for (WeakProblem problem : userReport.getProblemList()) {
					pointSum += problem.getPoint();
				}
				userReport.setPointSum(pointSum);
			}
			break;
		default:
			break;
		}

		Iterator<WeakProblem> iter = report.getNeedDeleteList().iterator();
		while (iter.hasNext()) {
			WeakProblem problem = iter.next();

			if (report.getReportType() == Constants.WEAK_REPORT_TYPE_YIXIANYUANGONG) {
				for (WeakUserReport userReport : report.getYixianReportList()) {
					if (userReport.getUserID().equals(problem.getWriteUserID()) && !problem.isNeedDelete()) {
						userReport.setPointSum(userReport.getPointSum() + problem.getPoint());
						userReport.getProblemList().add(problem);
						iter.remove();
						break;
					}
				}
			} else if (report.getReportType() == Constants.WEAK_REPORT_TYPE_GUANLIRENYUAN) {
				for (WeakUserReport userReport : report.getGuanliReportList()) {
					if (userReport.getUserID().equals(problem.getWriteUserID()) && !problem.isNeedDelete()) {
						userReport.setPointSum(userReport.getPointSum() + problem.getPoint());
						userReport.getProblemList().add(problem);
						iter.remove();
						break;
					}
				}
			} else if (report.getReportType() == Constants.WEAK_REPORT_TYPE_DANWEI) {
				for (WeakUserReport userReport : report.getYixianReportList()) {
					if (userReport.getUserID().equals(problem.getWriteUserID()) && !problem.isNeedDelete()) {
						userReport.setPointSum(userReport.getPointSum() + problem.getPoint());
						userReport.getProblemList().add(problem);
						iter.remove();
						break;
					}
				}
				for (WeakUserReport userReport : report.getGuanliReportList()) {
					if (userReport.getUserID().equals(problem.getWriteUserID()) && !problem.isNeedDelete()) {
						userReport.setPointSum(userReport.getPointSum() + problem.getPoint());
						userReport.getProblemList().add(problem);
						iter.remove();
						break;
					}
				}
				for (WeakUserReport userReport : report.getNotWrittenGuanliReportList()) {
					if (userReport.getUserID().equals(problem.getWriteUserID()) && !problem.isNeedDelete()) {
						userReport.setPointSum(userReport.getPointSum() + problem.getPoint());
						userReport.getProblemList().add(problem);
						iter.remove();
						break;
					}
				}
			}
		}

		if (clearNeedDelete) {
			report.getNeedDeleteList().clear();
		}
		return true;
	}

	private WeakReport createYixianyuangongReport(int year, int month, String dateStart, String dateEnd, Long depart1ID, Long equipID) {
		WeakDao weakDao = DaoCreator.createWeakDao();
		EquipmentDao equipDao = DaoCreator.createEquipmentDao();

		List<User> userList = equipDao.getEquipmentYixianyuangong(equipID);

		List<Problem> problemList = weakDao.getYixianyuangongProblemList(dateStart, dateEnd, equipID, userList);

		WeakReport weakReport = new WeakReport();
		weakReport.setReportType(Constants.WEAK_REPORT_TYPE_YIXIANYUANGONG);

		// init weak report
		weakReport.setYear(year);
		weakReport.setMonth(month);
		weakReport.setDepart1ID(depart1ID);
		weakReport.setEquipID(equipID);

		Map<Long, User> userMap = weakReport.getUserMap();
		for (User user : userList) {
			userMap.put(user.getId(), user);
		}
		weakReport.setUserMap(userMap);

		Map<Long, WeakUserReport> yixianReportMap = new HashMap<Long, WeakUserReport>();
		for (Problem p : problemList) {
			Long writeUserID = p.getWriteUserID();
			WeakUserReport userReport = yixianReportMap.get(writeUserID);
			if (userReport == null) {
				userReport = new WeakUserReport();
				userReport.setUserID(writeUserID);
				userReport.setPointSum(0);

				yixianReportMap.put(writeUserID, userReport);
			}

			int pointSum = userReport.getPointSum();

			WeakProblem weakProblem = new WeakProblem(p);
			initWeakProblem(weakProblem);

			pointSum += weakProblem.getPoint();

			userReport.setPointSum(pointSum);
			userReport.getProblemList().add(weakProblem);
		}

		List<WeakUserReport> yixianReportList = new ArrayList<WeakUserReport>(yixianReportMap.values());

		weakReport.setYixianReportList(yixianReportList);

		return weakReport;
	}

	private void initWeakProblem(WeakProblem weakProblem) {
		if (weakProblem.getProblemType() == null) {
			// hse problem
			weakProblem.setPoint(10 * 100);
		} else if (weakProblem.getProblemType() == Constants.PROBLEM_TYPE_POSTCHECK) {
			if (weakProblem.getGrade() == null || weakProblem.getGrade() == 0) {
				weakProblem.setPoint(10 * 100);
			} else {
				weakProblem.setPoint(weakProblem.getGrade() * 20);
			}
		}
		weakProblem.setWeakType(2);// default c

		weakProblem.setHasNoneWorkTime(false);
		weakProblem.setNoneWorkTime(null);
	}

	private void setWeakProblem(WeakProblem problem, Integer weakType) {
		if (weakType != null) {
			problem.setWeakType(weakType);

			if (problem.getProblemTitle() == null || (problem.getGrade() == null || problem.getGrade() == 0)) {
				// HSE problem or postcheck problem with grade is 0
				switch (weakType) {
				case 0:
					problem.setPoint(40 * 100);
					break;
				case 1:
					problem.setPoint(20 * 100);
					break;
				case 2:
					problem.setPoint(10 * 100);
					break;

				default:
					break;
				}
			}
		}
	}

	private WeakReport createGuanlirenyuanReport(int year, int month, String dateStart, String dateEnd, Long depart1ID, User user) {
		WeakDao weakDao = DaoCreator.createWeakDao();
		RoleDao roleDao = DaoCreator.createRoleDao();

		List<Problem> problemList = weakDao.getGuanlirenyuanProblemList(dateStart, dateEnd, user);

		Role userRole = roleDao.getRoleByUserID(user.getId());

		WeakReport weakReport = new WeakReport();
		weakReport.setReportType(Constants.WEAK_REPORT_TYPE_GUANLIRENYUAN);

		// init weak report
		weakReport.setYear(year);
		weakReport.setMonth(month);
		weakReport.setDepart1ID(depart1ID);
		weakReport.setUserID(user.getId());

		weakReport.getUserMap().put(user.getId(), user);

		WeakUserReport userReport = new WeakUserReport();
		userReport.setUserID(user.getId());
		userReport.setRoleType(userRole.getRoleType());
		userReport.setPointSum(0);

		String problemZhenggaiIssolved = MessageUtil.getMessage(Constants.PROBLEM_ZHENGGAI_ISSOLVED);
		String problemZhenggaiNone = MessageUtil.getMessage(Constants.PROBLEM_ZHENGGAI_NONE);
		for (Problem p : problemList) {
			int pointSum = userReport.getPointSum();

			WeakProblem weakProblem = new WeakProblem(p);

			initWeakProblem(weakProblem);

			if (weakProblem.getProblemType() != null && weakProblem.getProblemType() == Constants.PROBLEM_TYPE_POSTCHECK) {
				if (problemZhenggaiIssolved.equals(weakProblem.getProblemZhenggai())
						|| problemZhenggaiNone.equals(weakProblem.getProblemZhenggai())) {
					weakProblem.setNeedDelete(true);

					weakReport.getNeedDeleteList().add(weakProblem);
					continue;
				}
			}
			pointSum += weakProblem.getPoint();

			userReport.setPointSum(pointSum);
			userReport.getProblemList().add(weakProblem);
		}

		weakReport.getGuanliReportList().add(userReport);

		return weakReport;
	}

	private WeakReport createDanweiReport(int year, int month, String dateStart, String dateEnd, Long depart1ID) {
		WeakDao weakDao = DaoCreator.createWeakDao();
		UserDao userDao = DaoCreator.createUserDao();
		ProblemDao problemDao = DaoCreator.createProblemDao();

		WeakReport danweiReport = new WeakReport();
		danweiReport.setYear(year);
		danweiReport.setMonth(month);
		danweiReport.setDepart1ID(depart1ID);
		danweiReport.setReportType(Constants.WEAK_REPORT_TYPE_DANWEI);

		// prepare user list
		Map<Long, User> notWrittenGuanlirenyuan = new HashMap<Long, User>();
		List<User> userList = userDao.getUserListByDepartment1ID(depart1ID);
		for (User user : userList) {
			notWrittenGuanlirenyuan.put(user.getId(), user);
		}

		// store them in the report
		danweiReport.getUserMap().putAll(notWrittenGuanlirenyuan);

		Iterator<User> iter = notWrittenGuanlirenyuan.values().iterator();
		while (iter.hasNext()) {
			User user = iter.next();
			if ((user.getRoleType() & Constants.ROLE_TYPE_YIXIANYUANGONG) > 0) {
				iter.remove();
			}
		}

		List<WeakReport> reportList = weakDao.getWeakReportList(year, month, depart1ID);
		for (WeakReport report : reportList) {
			if (report.getProblemID() == null) {
				continue;
			}
			if (report.getReportType() == Constants.WEAK_REPORT_TYPE_YIXIANYUANGONG) {
				Problem p = problemDao.getProblemByID(report.getProblemID());
				if (p.getProcessStatus() >= Constants.PROBLEM_STATUS_CLOSE) {
					danweiReport.getYixianReportList().addAll(report.getYixianReportList());
				}

			} else if (report.getReportType() == Constants.WEAK_REPORT_TYPE_GUANLIRENYUAN) {
				danweiReport.getGuanliReportList().addAll(report.getGuanliReportList());
				notWrittenGuanlirenyuan.remove(report.getUserID());
			}
		}

		for (User notWrittenUser : notWrittenGuanlirenyuan.values()) {
			WeakReport realtimeReport = createGuanlirenyuanReport(year, month, dateStart, dateEnd, depart1ID, notWrittenUser);
			danweiReport.getNotWrittenGuanliReportList().addAll(realtimeReport.getGuanliReportList());
			danweiReport.getNeedDeleteList().addAll(realtimeReport.getNeedDeleteList());
		}

		return danweiReport;
	}

	public boolean refreshWeakReport(HttpServletRequest request) {
		HttpSession session = request.getSession();

		WeakReport report = (WeakReport) session.getAttribute(Constants.SESSION_WEAK_REPORT);
		if (report == null) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, "");
			return false;
		}

		Long problemID = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_PROBLEM_ID, (Long) null);
		Boolean needDelete = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_NEED_DELETE, (Boolean) null);
		Integer weakType = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_WEAK_TYPE, (Integer) null);
		Boolean hasNoneWorkTime = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_HAS_NONEWORKTIME, (Boolean) null);

		if (problemID == null) {
			return true;
		}
		WeakProblem problem = report.getProblem(problemID);
		WeakUserReport userReport = report.getUserReport(problemID);

		if (userReport != null && !problem.isNeedDelete()) {
			userReport.setPointSum(userReport.getPointSum() - problem.getPoint());
		}

		setWeakProblem(problem, weakType);

		String noneWorkTime = request.getParameter(Constants.REQUEST_PARAMETER_NONEWORKTIME);
		if (noneWorkTime != null) {
			noneWorkTime = Util.isoToUTF8(noneWorkTime);
			problem.setNoneWorkTime(noneWorkTime);
		}
		
		if (hasNoneWorkTime != null) {
			problem.setHasNoneWorkTime(hasNoneWorkTime);
			if (!hasNoneWorkTime) {
				//problem.setNoneWorkTime(null);
				problem.setPoint(problem.getPoint() / 2);
			} else {
				problem.setNoneWorkTime(noneWorkTime);
				problem.setPoint(problem.getPoint() * 2);
			}
		} else {
			if (problem.hasNoneWorkTime() && needDelete == null) {
				// double point only when check weak type
				problem.setPoint(problem.getPoint() * 2);
			}
		}

		if (userReport != null && !problem.isNeedDelete()) {
			userReport.setPointSum(userReport.getPointSum() + problem.getPoint());
		}

		if (needDelete != null) {
			problem.setNeedDelete(needDelete);

			if (userReport != null) {
				if (needDelete) {
					userReport.setPointSum(userReport.getPointSum() - problem.getPoint());
				} else {
					userReport.setPointSum(userReport.getPointSum() + problem.getPoint());
				}
			}
		}

		return true;
	}

	public boolean generateWeakCorpReport(HttpServletRequest request) {
		HttpSession session = request.getSession();

		DepartmentDao departDao = DaoCreator.createDepartmentDao();
		WeakDao weakDao = DaoCreator.createWeakDao();
		ProblemDao problemDao = DaoCreator.createProblemDao();

		Integer year = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_YEAR, (Integer) null);

		WeakCorpReport corpReport = new WeakCorpReport();
		corpReport.setYear(year);

		Long[] shiyebuIDs = new Long[] { Constants.SHIYEBU_ID_LIANYOU1, Constants.SHIYEBU_ID_LIANYOU2, Constants.SHIYEBU_ID_LIANYOU3,
				Constants.SHIYEBU_ID_LIANYOU4, Constants.SHIYEBU_ID_LIANYOU5, Constants.SHIYEBU_ID_HUAGONG1, Constants.SHIYEBU_ID_HUAGONG2,
				Constants.SHIYEBU_ID_HUAGONG3, Constants.SHIYEBU_ID_HUAGONG4, Constants.SHIYEBU_ID_CHUYUN1, Constants.SHIYEBU_ID_CHUYUN2,
				Constants.SHIYEBU_ID_DONGLIGUANLI, Constants.SHIYEBU_ID_SHUIWUGUANLI, Constants.SHIYEBU_ID_CHANPINFENXIFUWU };
		for (Long shiyebuID : shiyebuIDs) {
			Department1 shiyebu = departDao.getDepartment1ByID(shiyebuID);
			corpReport.getShiyebuList().put(shiyebuID, shiyebu);
			corpReport.getTable().put(shiyebuID, new Boolean[12]);
		}
		List<Department1> chushiList = departDao.getDepartment1List(Constants.DEPARTMENT_TYPE_JIGUAN);
		for (Department1 chushi : chushiList) {
			boolean notNeeded = false;
			for (Long chushiNoNeedIncluded : Constants.CHUSHI_IDS_WEAK) {
				if (chushiNoNeedIncluded.equals(chushi.getId())) {
					notNeeded = true;
					break;
				}
			}
			if (notNeeded) {
				continue;
			}
			corpReport.getChushiList().put(chushi.getId(), chushi);
			corpReport.getTable().put(chushi.getId(), new Boolean[12]);
		}
		List<WeakReport> departReportList = weakDao.getDanweiWeakReportListForCorp(year);
		for (WeakReport departReport : departReportList) {
			Long problemID = departReport.getProblemID();
			if (problemID == null) {
				continue;
			}

			Problem problem = problemDao.getProblemByID(problemID);
			if (problem.getProcessStatus() >= Constants.PROBLEM_STATUS_CLOSE) {
				Boolean[] tableArray = corpReport.getTable().get(departReport.getDepart1ID());
				int month = departReport.getMonth();
				tableArray[month - 1] = Boolean.TRUE;
			}
		}

		session.setAttribute(Constants.SESSION_WEAK_CORP_REPORT, corpReport);
		return true;
	}

	public boolean prepareWeakReport(HttpServletRequest request) {
		HttpSession session = request.getSession();

		User user = (User) session.getAttribute(Constants.SESSION_USER);
		int weakReportType = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_WEAK_REPORT_TYPE, 0);

		String dateStart = request.getParameter(Constants.REQUEST_PARAMETER_DATE_START);
		String dateEnd = request.getParameter(Constants.REQUEST_PARAMETER_DATE_END);

		Long depart1ID = user.getDepartment1ID();
		Long equipID = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_EQUIP_ID, (Long) null);

		Calendar calStart = Util.convertStrToDate(dateStart);
		int[] timeArray = Util.getDate(calStart);
		int year = timeArray[0], month = timeArray[1];

		WeakDao weakDao = DaoCreator.createWeakDao();
		WeakReport weakReport = null;
		switch (weakReportType) {
		case Constants.WEAK_REPORT_TYPE_YIXIANYUANGONG:
			weakReport = weakDao.getYixianyuangongWeakReport(year, month, equipID);
			if (weakReport != null) {
				session.removeAttribute(Constants.SESSION_WEAK_REPORT);
				return true;
			}

			weakReport = createYixianyuangongReport(year, month, dateStart, dateEnd, depart1ID, equipID);

			session.setAttribute(Constants.SESSION_WEAK_REPORT, weakReport);
			return true;
		case Constants.WEAK_REPORT_TYPE_GUANLIRENYUAN:
			weakReport = weakDao.getGuanlirenyuanWeakReport(year, month, user.getId());
			if (weakReport != null) {
				session.removeAttribute(Constants.SESSION_WEAK_REPORT);
				return true;
			}

			weakReport = createGuanlirenyuanReport(year, month, dateStart, dateEnd, depart1ID, user);
			session.setAttribute(Constants.SESSION_WEAK_REPORT, weakReport);
			return true;

		case Constants.WEAK_REPORT_TYPE_DANWEI:
			weakReport = weakDao.getDanweiWeakReport(year, month, depart1ID);
			if (weakReport != null) {
				session.removeAttribute(Constants.SESSION_WEAK_REPORT);
				return true;
			}

			weakReport = createDanweiReport(year, month, dateStart, dateEnd, depart1ID);
			session.setAttribute(Constants.SESSION_WEAK_REPORT, weakReport);
			return true;
		}

		return true;
	}
}
